package org.neuedu.ssmp.crm.controller;

import lombok.extern.slf4j.Slf4j;
import org.neuedu.ssmp.crm.entity.CustomerLost;
import org.neuedu.ssmp.crm.entity.Orders;
import org.neuedu.ssmp.crm.entity.vo.ResponseEntity;
import org.neuedu.ssmp.crm.service.ICustomerLostService;
import org.neuedu.ssmp.crm.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@RestController
@RequestMapping("/customerlost")
@Slf4j
public class CustomerLostController {

    @Autowired
    private ICustomerLostService customerLostService;

    @Autowired
    private IOrdersService ordersService;

    @RequestMapping("/queryAlls")
    public ResponseEntity QueryAlls(){
        List<Orders> orders = ordersService.queryTime();
        List<Integer> cuId = customerLostService.cId();
        List<Integer> cId = ordersService.cId();
        int nowDays = LocalDate.now().getYear()*365 +LocalDate.now().getMonthValue()*30 + LocalDate.now().getDayOfMonth()*1;
//        System.out.println(nowDays);
        try {
            for (int i = 0; i < cuId.size(); i++) {
                for (int j = 0; j < cId.size(); j++) {
                    if (cuId.get(i).equals(cId.get(j))){
                        System.err.println(cId.get(j));
                        cId.remove(cId.get(j));
                    }
                }
            }
            for (int i = 0; i < cId.size(); i++) {
//                System.out.println(cId.get(i));
                for (int j = 0; j < orders.size(); j++) {
                    if (cId.get(i).equals(orders.get(j).getCustomerId())){
//                        System.out.println(orders.get(j).getCustomerId());
                        int orderDays = orders.get(j).getOrderTime().getYear()*365+orders.get(j).getOrderTime().getMonthValue()*30+orders.get(j).getOrderTime().getDayOfMonth()*1;
                        if (nowDays - orderDays > 180){
                            customerLostService.inserts(orders.get(j).getCustomerId(), orders.get(j).getOrderTime());
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("放行");
        }
        return ResponseEntity.data(customerLostService.queryAll());
    }

    @RequestMapping("/queryAll")
    public ResponseEntity QueryAll() {
        return ResponseEntity.data(customerLostService.queryAll());
    }

    @PostMapping("/updateThing")
    public ResponseEntity Update(@RequestBody CustomerLost customerLost) {
//        @RequestParam(value = "id") int id,
//        @RequestParam(value = "content", required = false) String content,
//        @RequestParam(value = "addMeasures", required = false) String addMeasures,
//        @RequestParam(value = "lostCause", required = false) String lostCause,
//        @RequestParam(value = "state", required = false) String state,
//        @RequestParam(value = "lostDay", required = false) Date lostDay
//        System.out.println(lostDay);
//        Long dateNow = Long.parseLong(lostDay);
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        String data = sdf.format(new Date(Long.parseLong(String.valueOf(dateNow))));
        return ResponseEntity.data(customerLostService.updateById(customerLost));
    }

    @PostMapping("/searchSome")
    public ResponseEntity SearchSome(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "state", required = true) String state) {
        return ResponseEntity.data(customerLostService.vagueQusery(name, username, state));
    }

}
